﻿using KinonekoSoftware.Foundation.Platform.Graphics;
using KinonekoSoftware.Studio.Models.Utils;

namespace KinonekoSoftware.Studio.Components
{
    public partial class ImageEditorViewModel : ObjectDialog<ImageEditResult>
    {

        protected internal static async Task<Tuple<byte[], int, int>> Preprocessing(byte[] original, int w, int h)
        {
            //
            //
            if (w > 1920 ||
                h > 1080)
            {
                return await ResizeTo1080P(original, w, h);

            }

            return new Tuple<byte[], int, int>(original, w, h);
        }


        protected internal static Task<Tuple<byte[], int, int>> ResizeTo320P(byte[] original, int w, int h)
        {
            return Task.Run(() =>
            {
                var pip = Ioc.Get<IPlatformImageProcessor>();

                //
                //
                pip.Release();
                int w1, h1;

                if (w > 640)
                {
                    var d = (w / 640d);
                    w1 = 640;
                    h1 = (int)(h / d);
                }
                else
                {
                    var d = (h / 360d);
                    w1 = (int)(w / d);
                    h1 = 360;
                }

                pip.Resize(original, w1, h1, out var array);
                return new Tuple<byte[], int, int>(array, w1, h1);
            });

        }

        protected internal static Task<Tuple<byte[], int, int>> ResizeTo1080P(byte[] original, int w, int h)
        {
            return Task.Run(() =>
            {

                var pip = Ioc.Get<IPlatformImageProcessor>();

                //
                //
                pip.Release();
                int w1, h1;

                if (w > 1920)
                {
                    var d = (w / 1920d);
                    w1 = 1920;
                    h1 = (int)(h / d);
                }
                else
                {
                    var d = (h / 1080d);
                    w1 = (int)(w / d);
                    h1 = 1080;
                }

                pip.Resize(original, w1, h1, out var array);
                return new Tuple<byte[], int, int>(array, w1, h1);
            });

        }

        protected internal static Task<byte[]> ResizeTo256P(byte[] original)
        {
            return Task.Run(() =>
            {
                var pip = Ioc.Get<IPlatformImageProcessor>();

                //
                //
                pip.Release();

                pip.Resize(original, 256, 256, out var array);
                return array;
            });

        }

    }
}